#include"stdio.h"
#include"arm.h"
const char * mode_sym[]=
{
"user",
"irq",
"fiq",
"svc",
"abort",
"undefined",
"system"
};
void dump_arm_base_state(char* buffer,PARM_STATE p_state)
{
	int pos=0;

	int i;

	pos= sprintf(buffer,"R0-7:");
	for(i=0;i<8;i++)
		pos+=sprintf(buffer+pos,"%08x ",p_state->prf[i]);
	buffer[pos++]='\n';


	pos+= sprintf(buffer+pos,"8-15:");
	for(;i<15;i++)
		pos+=sprintf(buffer+pos,"%08x ",p_state->prf[PREG(i,p_state->mode)]);
	
	//buffer[pos++]='\n';	
//	pos+=sprintf(buffer+pos,"CPSR=%08x ",p_state->prf[CPSR]);
//	pos+=sprintf(buffer+pos,"(%s)",mode_sym[p_state->mode]);
	//for(;i<15;i++)
	//	pos+=sprintf(buffer+pos,"R%d=%08x ",i,p_state->prf[i]);
//	pos+=sprintf(buffer+pos,"PC=%08x ",p_state->prf[PC]);


	//if(TEST_BIT(p_state->prf[CPSR],CPSR_BIT_T))
	//pos+=sprintf(buffer+pos,"Thumb");
	if(TEST_BIT(p_state->prf[CPSR],CPSR_BIT_V)) buffer[pos++]='V';
	if(TEST_BIT(p_state->prf[CPSR],CPSR_BIT_N)) buffer[pos++]='N';
	if(TEST_BIT(p_state->prf[CPSR],CPSR_BIT_C)) buffer[pos++]='C';	
	if(TEST_BIT(p_state->prf[CPSR],CPSR_BIT_Z)) buffer[pos++]='Z';
	buffer[pos]=0;
}

void dump_arm_bank_state(char* buffer,PARM_STATE p_state)
{
	int pos=0;

	for(int i=8;i<13;i++)
		pos+=sprintf(buffer+pos,"FIQ_R%d=%x ",i,p_state->prf[i-8+R8_FIQ]);

	pos+=sprintf(buffer+pos,"\nFIQ_R13=%x ",p_state->prf[R13_FIQ]);
	pos+=sprintf(buffer+pos,"ABORT_R13=%x ",p_state->prf[R13_ABORT]);
	pos+=sprintf(buffer+pos,"IRQ_R13=%x ",p_state->prf[R13_IRQ]);
	pos+=sprintf(buffer+pos,"SVC_R13=%x ",p_state->prf[R13_SVC]);
	pos+=sprintf(buffer+pos,"UNDEF_R13=%x\n",p_state->prf[R13_UNDEF]);

	pos+=sprintf(buffer+pos,"FIQ_R14=%x ",p_state->prf[R14_FIQ]);
	pos+=sprintf(buffer+pos,"ABORT_R14=%x ",p_state->prf[R14_ABORT]);
	pos+=sprintf(buffer+pos,"IRQ_R14=%x ",p_state->prf[R14_IRQ]);
	pos+=sprintf(buffer+pos,"SVC_R14=%x ",p_state->prf[R14_SVC]);
	pos+=sprintf(buffer+pos,"UNDEF_R14=%x\n",p_state->prf[R14_UNDEF]);
}

void dump_arm_spsr_state(char* buffer,PARM_STATE p_state)
{
	int pos=0;
	pos+=sprintf(buffer+pos,"FIQ_SPSR=%x ",p_state->prf[SPSR_FIQ]);
	pos+=sprintf(buffer+pos,"ABORT_SPSR=%x ",p_state->prf[SPSR_ABORT]);
	pos+=sprintf(buffer+pos,"IRQ_SPSR=%x ",p_state->prf[SPSR_IRQ]);
	pos+=sprintf(buffer+pos,"SVC_SPSR=%x ",p_state->prf[SPSR_SVC]);
	pos+=sprintf(buffer+pos,"UNDEF_SPSR=%x\n",p_state->prf[SPSR_UNDEF]);
}




void dump_thumb_base_state(char* buffer,PARM_STATE p_state)
{
	int pos=0;

	int i;

	pos= sprintf(buffer,"R0-7:");
	for(i=0;i<8;i++)
		pos+=sprintf(buffer+pos,"%08x ",p_state->prf[i]);
	buffer[pos++]='\n';


	pos+=sprintf(buffer+pos,"sp=%08x ",p_state->prf[PREG(R13,p_state->mode)]);
	pos+=sprintf(buffer+pos,"lr=%08x ",p_state->prf[PREG(R14,p_state->mode)]);
	
	//buffer[pos++]='\n';	
	pos+=sprintf(buffer+pos,"CPSR=%08x ",p_state->prf[CPSR]);
	pos+=sprintf(buffer+pos,"(%s)",mode_sym[p_state->mode]);
	//for(;i<15;i++)
	//	pos+=sprintf(buffer+pos,"R%d=%08x ",i,p_state->prf[i]);
//	pos+=sprintf(buffer+pos,"PC=%08x ",p_state->prf[PC]);


	//if(TEST_BIT(p_state->prf[CPSR],CPSR_BIT_T))
	//pos+=sprintf(buffer+pos,"Thumb");
	
}